<template>
  <div
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-row class="padding-sm items-cente flex">
      <el-col :span="22" style="margin: 0 auto; padding-top: 10px">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>商品分类列表</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
      <el-col :span="2" style="margin: 0 auto">
        <el-button
          type="primary"
          size="small"
          round
          plain
          @click="showadd = true"
        >
          添加分类
        </el-button>
      </el-col>
    </el-row>
    <div class="contion">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="_id" label="ID"> </el-table-column>
        <el-table-column prop="label" width="200" label="分类名称">
        </el-table-column>
        <el-table-column align="right" width="150" label="操作">
          <template slot-scope="scope">
            <el-popconfirm title="确定删除次分类吗" @confirm="deleteRow(scope)">
              <el-button slot="reference" type="text" size="small"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- /* 添加分类 */ -->
    <el-dialog title="提示" :visible.sync="showadd" width="30%">
      <el-input type="text" v-model="addtext" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="showadd = false">取 消</el-button>
        <el-button type="primary" @click="addtexts">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import business from "../../utils/business";
export default {
  data() {
    return {
      msg: "this is cancation",
      tableData: [],
      //   提示窗口
      showadd: false,
      //   添加的内容
      addtext: "",
      loading: true,
    };
  },

  methods: {
    deleteRow(e, b) {
      this.tableData = this.tableData.filter((res) => res.uid !== e.row.uid);
      this.$message.success("删除成功");
    },
    addtexts() {
      try {
        this.tableData.forEach((element) => {
          var tabName = element.name;
          if (tabName === this.addtext) {
            this.$message.warning("已经有这个商品了!");
            throw Error();
          } else {
            let date = new Date().getTime();
            this.tableData.unshift({ uid: date, name: this.addtext });
            setTimeout(() => {
              this.$message.success("添加成功");
              this.showadd = false;
              this.addtext = "";
            }, 300);
            throw Error();
          }
        });
      } catch (error) {
        this.$message.error(error);
      }
    },
  },
  created() {
    // 查询菜品分类
    business
      .foodtype({})
      .then((res) => {
        this.tableData = res.data.data;
        this.loading = false;
      })
      .catch((error) => {
        this.$message.error("登录已过期");
        this.$store.commit("$_remobeStorage")
        return error;
      });
  },
};
</script>

<style lang="less" scoped>
.contion {
  width: 60%;
  margin: 0 auto;
}
</style>